<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 添加试题按钮 -->
        <div class="addQusBtn_box">
          <el-button
            type="success"
            icon="el-icon-edit"
            style="text-align:right"
            size="small"
            @click="$router.push('/questions/new')"
            >新增试题</el-button
          >
        </div>
        <!-- form表单 -->
        <el-form :inline="true" :model="serachForm" ref="serachFormRef" class="demo-form-inline">
          <!-- 第一行 -->
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科" label-width="80px">
                <el-select v-model="serachForm.subjectID" placeholder="请选择" size="small" @change="subjectIDChange()">
                  <el-option
                    v-for="(item, index) in subject"
                    :key="index"
                    :label="item.label"
                    :value="String(item.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录" label-width="80px">
                <el-select v-model="serachForm.catalogID" placeholder="请选择" size="small">
                  <el-option
                    v-for="(item, indexd) in directoryList"
                    :key="indexd"
                    :label="item.directoryName"
                    :value="item.directoryName"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签" label-width="80px">
                <el-select v-model="serachForm.tags" placeholder="请选择" size="small">
                  <el-option
                    v-for="item in tagsList"
                    :key="item.id"
                    :label="item.tagName"
                    :value="item.tagName"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字" label-width="80px">
                <el-input v-model="serachForm.keyword"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第一行结束 -->
          <!-- 第二行 -->
          <el-row>
            <el-col :span="6">
              <el-form-item label="试题类型" label-width="80px">
                <el-select v-model="serachForm.questionType" placeholder="请选择" size="small">
                  <el-option
                    v-for="(item, index) in questionTypeArr"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="难度" label-width="80px">
                <el-select v-model="serachForm.difficulty" placeholder="请选择" size="small">
                  <el-option
                    v-for="(item, index) in difficultyArr"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向" label-width="80px">
                <el-select v-model="serachForm.direction" placeholder="请选择" size="small">
                  <el-option v-for="(item, index) in directionArr" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="录入人" label-width="80px">
                <el-select v-model="serachForm.creatorID" placeholder="请选择" size="small">
                  <el-option v-for="(item, index) in creatorArr" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第二行结束 -->
          <!-- 第三行 -->
          <el-row>
            <el-col :span="6">
              <el-form-item label="标题备注" label-width="80px">
                <el-input v-model="serachForm.remarks"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="企业简称" label-width="80px">
                <el-input v-model="serachForm.shortName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="城市" label-width="80px" class="city">
                <el-select
                  v-model="serachForm.province"
                  placeholder="请选择"
                  size="small"
                  style="width:49%;margin-right:2%"
                  @change="provinceChange"
                >
                  <el-option
                    v-for="(item, index) in provincesList"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
                <el-select v-model="serachForm.city" placeholder="请选择" size="small" style="width:49%">
                  <template v-if="serachForm.province">
                    <el-option v-for="(item, index) in cityList" :key="index" :label="item" :value="item"></el-option>
                  </template>
                  >
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="clearAndSearch">
                <el-button size="small" @click="clearForm">清除</el-button>
                <el-button type="primary" size="small" @click="getQuesList">搜索</el-button>
              </div>
            </el-col>
          </el-row>
          <!-- 第三行结束 -->
        </el-form>
        <!-- 数据提示 -->
        <el-alert class="alert" :title="`共有${total}条数据`" type="info" show-icon> </el-alert>
        <!-- 列表 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="number" label="试题编号" width="120"> </el-table-column>
          <el-table-column prop="subject" label="学科" width="100"> </el-table-column>
          <el-table-column prop="catalog" label="目录" width="100"> </el-table-column>
          <el-table-column prop="questionType" label="题型" width="100">
            <template v-slot="scoped">
              {{ scoped.row.questionType | handelQuestionType }}
            </template>
          </el-table-column>
          <el-table-column prop="question" label="题干">
            <template v-slot="scoped">
              <div v-html="scoped.row.question"></div>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="160">
            <template v-slot="scoped">
              {{ scoped.row.addDate | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度" width="100">
            <template v-slot="scoped">
              {{ scoped.row.difficulty | handelDifficulty }}
            </template>
          </el-table-column>
          <el-table-column prop="creator" label="录入人" width="100"> </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scoped">
              <el-row class="btn_row">
                <el-button
                  type="primary"
                  icon="el-icon-view"
                  @click="preview(scoped.row)"
                  title="预览"
                  circle
                  plain
                ></el-button>
                <el-button
                  type="success"
                  icon="el-icon-edit"
                  @click="$router.push(`/questions/new?id=${scoped.row.id}`)"
                  title="修改"
                  circle
                  plain
                ></el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="deleteQues(scoped.row)"
                  title="删除"
                  circle
                  plain
                ></el-button>
                <el-button
                  type="warning"
                  icon="el-icon-check"
                  @click="addToChoice(scoped.row)"
                  title="加入精选"
                  circle
                  plain
                ></el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          class="page"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="  prev, pager,sizes, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
      <questionDialog :dialogShow.sync="dialogShow" :rowData="rowData" />
    </div>
  </div>
</template>

<script>
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js'
import { simple } from '@/api/hmmm/subjects.js'
import { list as directoryListApi } from '@/api/hmmm/directorys.js'
import { list as tagListApi } from '@/api/hmmm/tags.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
export default {
  components: {
    questionDialog: () => import('@/module-hmmm/components/questions-preview.vue')
  },
  data() {
    return {
      serachForm: {
        difficulty: '', // 难度
        questionType: '', // 试题类型
        tags: '', // 标签名称
        keyword: '', // 关键字
        catalogID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '',
        city: ''
      },
      total: 5,
      tableData: [], // table数据李彪
      currentPage: 1, // 当前页
      pageSize: 5, // 当前页容量
      subject: [], // 学科数组
      directoryList: [], // 二级目录数组
      tagsList: [], // 标签数据
      questionTypeArr: questionType, // 问题类型数组
      difficultyArr: difficulty, // 困难程度数据
      directionArr: direction, // 方向数组
      creatorArr: [], // 录入人数组
      provincesList: [],
      cityList: [],
      dialogShow: false, // 对话框控制
      rowData: ''
    }
  },

  created() {
    // 初始化得到所以基础试题数据
    this.getQuesList()
    // 初始化得到学科数据
    this.getSubjectSimpleData()
    this.provincesList = provinces()
  },
  watch: {},
  methods: {
    // 获取table数据
    async getQuesList() {
      const res = await list({
        page: this.currentPage,
        pagesize: this.pageSize,
        ...this.serachForm
      })
      // const result = await detail(res.data.items[1])
      this.tableData = res.data.items
      this.total = res.data.counts
      // 获取录入人数组
      this.creatorArr = this.handleRepeat(this.tableData)
      // console.log(this.tableData, this.creatorArr)
    },
    // 获取学科简单列表数据
    async getSubjectSimpleData() {
      const res = await simple()
      this.subject = res.data
      // console.log(res.data, 'getSubjectSimpleData')
    },
    // 获取二级目录数据
    async getDirectoryListData() {
      const res = await directoryListApi({
        subjectID: this.serachForm.subjectID
      })
      this.directoryList = res.data.items
      console.log(res, 'getDirectoryListData')
    },
    // 获取标签列表数据
    async getTagListData() {
      const res = await tagListApi({
        subjectID: this.serachForm.subjectID
      })
      this.tagsList = res.data.items
      console.log(res)
    },
    handleSizeChange(size) {
      this.pageSize = size
      this.getQuesList()
      console.log(size)
    },
    handleCurrentChange(page) {
      this.currentPage = page
      this.getQuesList()
      console.log(page)
    },
    // 学科select的change事件
    subjectIDChange() {
      if (this.serachForm.subjectID) {
        // 选中学科后,得到二级目录数据
        this.getDirectoryListData()
        //  选中学科后,得到标签数据
        this.getTagListData()
      } else {
        this.directoryList = []
      }
      console.log(this.serachForm.subjectID)
    },
    // 省份change事件
    provinceChange() {
      console.log(this.serachForm.province, 1)
      this.cityList = citys(this.serachForm.province)
      console.log(this.cityList)
    },
    // 录入人数组去重
    handleRepeat(value) {
      const arr = value.map(item => {
        return item.creator
      })
      const newArr = []
      arr.forEach(item => {
        if (newArr.indexOf(item) === -1) newArr.push(item)
      })
      return newArr
    },
    // 清除表单
    clearForm() {
      this.serachForm = {
        difficulty: '', // 难度
        questionType: '', // 试题类型
        tags: '', // 标签名称
        keyword: '', // 关键字
        catalogID: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '',
        city: ''
      }
      // 清除之后重新渲染
      this.getQuesList()
    },
    // 预览点击
    preview(rowData) {
      this.rowData = rowData
      console.log(rowData)
      this.dialogShow = true
    },
    // 删除
    async deleteQues(rowList) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message.success('删除成功!')
          await remove(rowList)
          this.getQuesList()
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    addToChoice(rowList) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          this.$message.success('添加成功!')
          await choiceAdd({
            choiceState: '1',
            ...rowList
          })
          console.log(rowList)
          this.getQuesList()
        })
        .catch(() => {
          this.$message.info('已取消添加')
        })
    }
  },
  filters: {}
}
</script>

<style lang="scss" scoped>
.addQusBtn_box {
  text-align: right;
  margin-bottom: 20px;
}
.alert {
  margin-bottom: 10px;
}
::v-deep .el-form-item__content {
  width: 100%;
}
::v-deep .el-form-item.el-form-item--medium {
  display: flex;
  // border: 1px solid rgb(255, 88, 88);
  .el-select.select_style.el-select--small {
    width: 100%;
  }
}
::v-deep el-select .el-select--medium {
  width: 100%;
}
.el-select.el-select--small {
  width: 100%;
}
.el-col.el-col-6 {
  height: 51px;
}
::v-deep .el-form--inline .el-form-item {
  margin-left: 10px;
  margin-right: 0;
}
::v-deep .city {
  display: flex !important;
  el-select .el-select--medium {
    width: 50%;
  }
}
.clearAndSearch {
  float: right;
}
.btn_row {
  display: flex;
}
.page {
  float: right;
  margin: 20px 10px;
}
::v-deep label.el-form-item__label {
  min-width: 80px;
}
</style>
